<?php

class MemberGroupAction extends PublicAction {

    function index() {

        $list = M('MemberGroup')->select();
        $this->assign('list', $list);
        $this->display();
    }

    function add()
    {
        $access = M('Access');

        $node = M('Node');

        $r = $node->where("pid=0 and status=1")->select();

        $this->assign('topnode', $r);

        $groups[0] = array('id'=>0,'name'=>L('ACCESS_PUBLIC'));
        foreach($this->menudata as $key=>$r){
          if($r['parentid'] == 0)
          $groups[$r[id]]=$r;
        }
        $this->assign('groups', $groups);

        foreach($groups as $key=>$res){
          $result=$node->where("groupid=$res[id] and status=1")->select();
          $array=array();
          foreach($result as $r) {
            $r['parentid'] = $r['pid'];
            $array[] = $r;
          }
          $nodes[$res['id']]['data'] = $array;
          $nodes[$res['id']]['groupinfo'] = $res;
        }

        $node_app = $access->where("pid=0")->select();
        $this->assign('node_app', $node_app);

        $this->assign('alist', $alist);
        $this->assign('node', $nodes);

        $role = M('Role');
        $pk = ucfirst($role->getPk());
        $id = $_REQUEST[$role->getPk()];

        $do = 'getBy'.$pk;
        $vo = $role->$do( $id );

        if($vo['setup'])
          $vo['setup'] = string2array($vo['setup']);

        $this->assign('vo', $vo);
        $this->display();
    }


    function edit()
    {

        $model = M('MemberGroup');

        $id = $_GET['id'];

        $vo = $model->find($id);

        $this->assign('vo', $vo);
        $this->display();
    }

    function update()
    {

        $model = D('MemberGroup');

        if (false === $model->create()) {
          $this->error( $model->getError() );
        }

        if ($model->save()) {
            $this->redirect('index');
        }else{
            $this->error('修改失败！');
        }
    }
}